<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/8/3
 * Time: 13:08
 */

namespace app\admin\controller;


use app\model\CategoryModel;
use app\model\WareModel;
use think\facade\Request;
use think\facade\Validate;
use think\facade\View;

class Ware
{
    // 商品列表
    public function index()
    {
        $wareList = WareModel::select();

        foreach ($wareList as &$row){
            $category = CategoryModel::find($row['category_id']);
            $row['category_name'] = $category['category_name'];     //商品名称赋值给当前订单
        }
        unset($row);
        View::assign('wareList',$wareList);
        return View::fetch();
    }

    public function add()
    {
        $categoryList = CategoryModel::getAllPreKey();
        return View::fetch('', ['categoryList' => $categoryList]);
    }

    public function addSave()
    {
        // 获取参数
        // 校验参数
        // 插入数据
        $param = Request::param();// 获取数据
        // 加规则
        $validate = Validate::rule([
            'category_id|所属分类id' => 'require|between:1,'.PHP_INT_MAX,
            'ware_name|商品名称' => 'require|min:1|max:40',
            'ware_image|商品图片' => 'require|min:1|max:400',
            'ware_describe|商品描述' => 'require|min:1|max:400',
            'ware_type|商品规格' => 'require|min:1|max:3',
            'ware_price|商品价格' => 'require|min:1|max:5',
            'ware_num|商品销量' => 'require|min:1|max:5'
        ]);
        if (!$validate -> check($param)) {
            echo $validate->getError();
            exit();
        }

        // 连接数据库，把数据传到数据库
        $param['add_time'] = time();
        $param['update_time'] = time();
        $result = WareModel::create($param);
        return View::fetch('public/tips', ['result' => $result]);
    }

    public function edit()
    {
        $wareId = Request::param('ware_id');
        $ware = WareModel::find($wareId);

        $validate = Validate::rule([
            'ware_id|商品id' => 'require|between:1,'.PHP_INT_MAX,
        ]);

        if (!$validate->check(['ware_id'=>$wareId])) {
            echo $validate->getError();
            exit();
        }

        if (!$ware) {
            echo "商品不存在";
            exit();
        }
        $categoryList = CategoryModel::getAllPreKey();
        return View::fetch('',['ware'=>$ware,'categoryList' => $categoryList]);
    }

    public function editSave()
    {
        // 获取参数
        // 校验参数
        // 插入数据
        $param = Request::param();// 获取数据
        $validate = Validate::rule([
            'ware_id|商品id' => 'require|between:1,'.PHP_INT_MAX,
            'category_id|所属分类id' => 'require|between:1,'.PHP_INT_MAX,
            'ware_name|商品名称' => 'require|min:1|max:40',
            'ware_describe|商品描述' => 'require|min:1|max:400',
            'ware_type|商品规格' => 'require|min:1|max:3',
            'ware_price|商品价格' => 'require|min:1|max:5',
            'ware_num|商品销量' => 'require|min:1|max:5'

        ]);
        if (!$validate -> check($param)) {
            echo $validate->getError();
            exit();
        }

        // 连接数据库，把数据传到数据库
        $param['update_time'] = time();
        $result = WareModel::update($param);
        return View::fetch('public/tips', ['result' => $result]);
    }

    public function delete()
    {
        if (session('power') < 3) {
            return View::fetch('public/tips2');
        }
        $wareId = Request::param("ware_id");
        $validate = Validate::rule([
            'ware_id|商品id' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['ware_id' => $wareId])) {
            echo $validate->getError();
            exit();
        }

        $result = WareModel::destroy($wareId);
        return View::fetch('public/tips', ['result' => $result]);
    }
}